args t tokens cutter kick_weird where model longer when comp done dep 



cd "`comp'"


if `done' == 0 {


	foreach mod in `model' {
		

		local so text
		
		if substr("`model'", -7,7) == "single_" {
			local candidates neighbor Logit neural adaboost randomforest
			local so neighbor
			
		}
		else if `longer' == 1  {
			
			
			local candidates text dummies mine   silverstone mine_gold commodity commodity_alone commodity_short  commodity_short_alone commodity_region  commodity_region_alone
		}
		else  {		
			local candidates text dummies mine
		}



		foreach r in 1  {
			


			clear all


			forval y = 2000/2025 {
		
				forval m = 1/12 {
				
					
					capture confirm file "temp/`so'`where'`mod'`tokens'_`dep'`cutter'_topics`t'_m`m'_y`y'_g`when'.csv"
					display "temp/`so'`where'`mod'`tokens'_`dep'`cutter'_topics`t'_m`m'_y`y'_g`when'.csv"
				
					display _rc
					local cands
					if _rc==0 {
						foreach j in `candidates' {
							clear all
							capture confirm file "temp/`j'`where'`mod'`tokens'_`dep'`cutter'_topics`t'_m`m'_y`y'_g`when'.csv"
							display "temp/`j'`where'`mod'`tokens'_`dep'`cutter'_topics`t'_m`m'_y`y'_g`when'.csv"
							display _rc
							
							if _rc==0 {
								local cands `cands' `j'
								insheet using "temp/`j'`where'`mod'`tokens'_`dep'`cutter'_topics`t'_m`m'_y`y'_g`when'.csv"
								erase "temp/`j'`where'`mod'`tokens'_`dep'`cutter'_topics`t'_m`m'_y`y'_g`when'.csv"
								drop if _n == 1
								drop v1
								rename v3 isocode
								rename v2 `j'
								save temp/`j', replace
										
							}
						}
						clear
						foreach j in `cands' {
							capture confirm file "temp/`j'.dta"
							if _rc==0 {
								append using temp/`j'
								erase temp/`j'.dta	
							}		
						}
					
						collapse (max) `cands', by(isocode)
					
					
					
						gen dep = "`dep'"
						gen year = `y'

					
					
						gen month = `m'
				
						sort isocode year month
						
				
						save tokens_`tokens'_`dep'`cutter'_topics`t'_m`m'_y`y'_`when', replace
					}

			
					
				}
					
				
			}
						
					
					
			clear all




			
			forval y = 2000/2025 {
				local start_m 1
			
				forval m = 1/12 {
					
					capture confirm file "tokens_`tokens'_`dep'`cutter'_topics`t'_m`m'_y`y'_`when'.dta"
					display "tokens_`tokens'_`dep'`cutter'_topics`t'_m`m'_y`y'_`when'"
					display _rc
					
					if _rc==0 {

						append using tokens_`tokens'_`dep'`cutter'_topics`t'_m`m'_y`y'_`when'
						erase tokens_`tokens'_`dep'`cutter'_topics`t'_m`m'_y`y'_`when'.dta
					}
				}
			}

			save tokens_`tokens'_topics`t'_`when', replace


			merge 1:1 isocode year month using masterfile_CR, keepusing(ons* armedconf* anyviolence* civilwar*  best *_dp) 
			drop if _merge == 2
			drop _merge

				
			sort isocode
			merge isocode using "country_region.dta" 
			drop if _merge==2
			
			drop _merge

			sort isocode year month
			keep dep isocode year month  `candidates' ons_*  anyviolence anyviolence1 armedconf1 region_o subregion_o best *_dp




			save `dep', replace

		

		}


	}

}


*merge all of them
if `done' == 1 {

	

	clear all
	
	
	local depos = subinstr("`dep'","?"," ",.)

	foreach d in `depos' {
		append using `d'
		erase `d'.dta
	}

	

	outsheet using `model'`tokens'_`cutter'_`kick_weird'_`where'_topics`t'_`longer'_`when'.csv, comma replace
	
}



